package com.jiyun.jiyunmall.member.controller;

import com.jiyun.jiyunmall.common.annotation.LogOperation;
import com.jiyun.jiyunmall.common.constant.Constant;
import com.jiyun.jiyunmall.common.dto.auth.MemberDTO;
import com.jiyun.jiyunmall.common.exception.BizCodeEnum;
import com.jiyun.jiyunmall.common.page.PageData;
import com.jiyun.jiyunmall.common.utils.ExcelUtils;
import com.jiyun.jiyunmall.common.utils.Result;
import com.jiyun.jiyunmall.common.validator.AssertUtils;
import com.jiyun.jiyunmall.common.validator.ValidatorUtils;
import com.jiyun.jiyunmall.common.validator.group.AddGroup;
import com.jiyun.jiyunmall.common.validator.group.DefaultGroup;
import com.jiyun.jiyunmall.common.validator.group.UpdateGroup;
import com.jiyun.jiyunmall.member.dto.CouponDTO;
import com.jiyun.jiyunmall.member.dto.SocialUser;
import com.jiyun.jiyunmall.member.entity.MemberEntity;
import com.jiyun.jiyunmall.member.excel.MemberExcel;
import com.jiyun.jiyunmall.member.exception.PhoneExistException;
import com.jiyun.jiyunmall.member.exception.UserNameExistException;
import com.jiyun.jiyunmall.member.feign.CouponFeign;
import com.jiyun.jiyunmall.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 会员
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-07-12
 */
@RestController
@RequestMapping("member/member")
@Api(tags="会员")
public class MemberController {
    @Autowired
    CouponFeign couponFeign;
    @Autowired
    private MemberService memberService;
    @PostMapping("/oauth2/login")
    public  Result<MemberDTO> oauth2Login(@RequestBody SocialUser socialUser) throws Exception {
        // 社交登录，如果第一次登录，则自动注册，保存社交登录的一些信息，到数据表中
        MemberEntity entity = memberService.login(socialUser);
        if (entity != null) {
            Result<MemberDTO> result = new Result<>();
            MemberDTO memberDTO = new MemberDTO();
            BeanUtils.copyProperties(entity,memberDTO);
            result.setData(memberDTO);
            return result;
        } else {
            Result<MemberDTO> result = new Result<>();
            result.setCode(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode());
            result.setMsg(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
            return result;
        }
    }
    @PostMapping("/login")
    public Result<MemberDTO> login(@RequestBody MemberDTO memberDTO)
    {
        MemberEntity entity = memberService.login(memberDTO);
        if (entity != null) {
            Result<MemberDTO> objectResult = new Result<>();
            MemberDTO memberDTO1 = new MemberDTO();
            BeanUtils.copyProperties(entity,memberDTO1);
            objectResult.setData(memberDTO1);
            return objectResult;
        } else {
            Result<MemberDTO> result = new Result<>();
            result.setCode(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode());
            result.setMsg(BizCodeEnum.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
            return result;
        }
    }
    @PostMapping("/regist")
    public Result regist(@RequestBody MemberDTO memberDTO) {
            Result<String> result = new Result<>();
        try {
            //调用Service去完成注册功能，说白了就是向member表写入一条数据
            memberService.regist(memberDTO);
        } catch (PhoneExistException e) {
            result.setCode(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode());
            result.setMsg(BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        } catch (UserNameExistException e) {
            result.setCode(BizCodeEnum.USER_EXIST_EXCEPTION.getCode());
            result.setMsg(BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }
        return  result;
    }
    @GetMapping("/getcoupon")
    public Result<PageData<CouponDTO>>  getcoupon(){
        Map<String, Object>  objectObjectHashMap = new HashMap<>();
        Result<PageData<CouponDTO>> page = couponFeign.page(objectObjectHashMap);
        System.out.println(page);
        return page;

    }
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<MemberDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<MemberDTO> page = memberService.page(params);

        return new Result<PageData<MemberDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<MemberDTO> get(@PathVariable("id") Long id){
        MemberDTO data = memberService.get(id);

        return new Result<MemberDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody MemberDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        memberService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody MemberDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        memberService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        memberService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<MemberDTO> list = memberService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, MemberExcel.class);
    }

}